package com.medicine.hbp.ui;

import org.kymjs.kjframe.KJBitmap;
import org.kymjs.kjframe.bitmap.BitmapCallBack;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;

import android.content.Intent;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.medicine.hbp.R;
import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.findInitActivityVo;
import com.medicine.hbp.sp.DadosSp;
import com.medicine.hbp.sp.InterestSp;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.sp.UtilsSp;
import com.medicine.hbp.uitl.ShortCut;
import com.medicine.hbp.uitl.ToastUtil;

/**
 * 展示页
 *
 * @author Administrator
 */
public class SplashActivity extends BaseActivity {
	@BindView(id=R.id.img_bg)public ImageView img_bg;
	@BindView(id=R.id.txt_time)public TextView txt_time;
	private int type=0;

//	private Handler handler = new Handler() {
//		@Override
//		public void handleMessage(Message msg) {
//			super.handleMessage(msg);
//			Intent intent =new Intent(aty, AndyViewPagerActivity.class);
//			startActivity(intent);
//			finish();
//		}
//	};

	private TimeCount time;
	private KJBitmap bitmap;
	private Handler handler2 = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if(type==0){
				Intent intent = new Intent(aty, MainActivity.class);
				startActivity(intent);
			}
			finish();
		}
	};
	public static SplashActivity splashActivity;
	@Override
	public void setRootView() {
		super.setRootView();
		splashActivity=this;
		// MyApplication.getMyApplication().setCookitString("");
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);// 去掉信息栏
		setContentView(R.layout.activity_splash);
		time = new TimeCount( 5000, 1000 );
	}
	@SuppressWarnings("deprecation")
	@Override
	public void initWidget() {
		super.initWidget();
		img_bg.setVisibility(View.GONE);
		bitmap=new KJBitmap();
		InterestSp.setInterestType(0, aty);
		ShortCut.createShortCut(aty);
		if (UtilsSp.getIsOne(aty)) {//非首次登陆
//			if(!TextUtils.isEmpty(DadosSp.getSplashUrl(aty))){
//				bitmap.display(img_bg, DadosSp.getSplashImg(aty),new BitmapCallBack() {
//					@Override
//					public void onFinish() {
//						super.onFinish();
//						img_bg.setVisibility(View.VISIBLE);
//						txt_time.setVisibility(View.VISIBLE);
//						img_bg.setOnClickListener(new OnClickListener() {
//							@Override
//							public void onClick(View arg0) {
//								type=1;
//								Intent intent=new Intent(aty,BannerWebActivity.class);
//								if(TextUtils.isEmpty(LoginSp.getToken(aty))){
//									intent.putExtra("URL",DadosSp.getSplashUrl(aty));
//								}else{
//									intent.putExtra("URL",DadosSp.getSplashUrl(aty)+"&token="+ LoginSp.getToken(aty));
//								}
//								intent.putExtra("NAME","积分活动");
//								intent.putExtra("type",1);
//								aty.startActivity(intent);
//							}
//						});
//						txt_time.setOnClickListener(new OnClickListener() {
//
//							@Override
//							public void onClick(View arg0) {
//								type=1;
//								Intent intent = new Intent(aty, MainActivity.class);
//								startActivity(intent);
//								finish();
//							}
//						});
//					}
//				});
//
//			}
			httpSplash();
		}else{
			Intent intent =new Intent(aty, AndyViewPagerActivity.class);
			startActivity(intent);
			finish();
		}
	}
	public void  httpSplash(){
		time.start();
		HttpParams params= new HttpParams();
		AppHttps.HttpPost(aty, HttpInterface.FINDINIT_ACTIVITY,params, new AppHttpCallBack<findInitActivityVo>(findInitActivityVo.class) {

			@SuppressWarnings("deprecation")
			@Override
			public void onSuccess(final findInitActivityVo result) {
				try{
					img_bg.setVisibility(View.GONE);
					bitmap.display(img_bg, result.info.initActivityImgUrl,new BitmapCallBack() {
						@Override
						public void onFinish() {
							super.onFinish();
							img_bg.setVisibility(View.VISIBLE);
//							txt_time.setVisibility(View.VISIBLE);
							if(!TextUtils.isEmpty(result.info.initActivityUrl)){
								txt_time.setVisibility(View.VISIBLE);
								img_bg.setOnClickListener(new OnClickListener() {
									@Override
									public void onClick(View arg0) {
										type=1;
										Intent intent=new Intent(aty,BannerWebActivity.class);
										if(TextUtils.isEmpty(LoginSp.getToken(aty))){
											intent.putExtra("URL",result.info.initActivityUrl);
										}else{
											intent.putExtra("URL",result.info.initActivityUrl+"&token="+ LoginSp.getToken(aty));
										}

										intent.putExtra("NAME","积分活动");
										intent.putExtra("type",1);
										aty.startActivity(intent);
									}

								});
								txt_time.setOnClickListener(new OnClickListener() {
									@Override
									public void onClick(View arg0) {
										type=1;
										Intent intent = new Intent(aty, MainActivity.class);
										startActivity(intent);
										finish();
									}
								});
							}

						}
					});
					if(null!=aty){
						DadosSp.setSplashImg(aty, result.info.initActivityImgUrl);
						DadosSp.setSplashUrl(aty, result.info.initActivityUrl);
					}
				}catch(Exception e){

				}

			}

			@Override
			public void onFailures(int errorCode, String errorMsg) {
				// TODO Auto-generated method stub

			}
		});
	}
	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super( millisInFuture, countDownInterval );
		}

		@Override
		public void onFinish() {// 计时完毕
			if(null!=this){
				txt_time.setText( String.valueOf(0)+" 跳过"  );
				Message msg = new Message();
				handler2.sendMessageDelayed(msg, 0);
			}
		}

		@Override
		public void onTick(long millisUntilFinished) {// 计时过程
			txt_time.setText( String.valueOf(millisUntilFinished / 1000)+" 跳过"  );
		}
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		time.cancel();
	}

}
